<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>vue拦截器! | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="什么是VUE拦截器拦截器分为请求拦截器和响应拦截器，在请求或响应被then或catch处理前拦截他们 拦截器的作用 添加统一的request的参数   比如客户端需要实现token的验证机制，比如你可以写$axios.get(‘&#x2F;files’, params)，拦截器帮你拼接成 http:&#x2F;&#x2F;127.0.0.1:8000&#x2F;files 这样的请求地址   处理统一的responseError">
<meta property="og:type" content="article">
<meta property="og:title" content="vue拦截器!">
<meta property="og:url" content="https://mourning-soul.gitee.io/hexo/2021/01/25/vue%E6%8B%A6%E6%88%AA%E5%99%A8/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="什么是VUE拦截器拦截器分为请求拦截器和响应拦截器，在请求或响应被then或catch处理前拦截他们 拦截器的作用 添加统一的request的参数   比如客户端需要实现token的验证机制，比如你可以写$axios.get(‘&#x2F;files’, params)，拦截器帮你拼接成 http:&#x2F;&#x2F;127.0.0.1:8000&#x2F;files 这样的请求地址   处理统一的responseError">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-25T10:00:00.000Z">
<meta property="article:modified_time" content="2021-01-25T10:16:33.670Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="-blocker">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/hexo/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/hexo/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/hexo/css/style.css">

  
    
<link rel="stylesheet" href="/hexo/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/hexo/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/hexo/">Home</a>
        
          <a class="main-nav-link" href="/hexo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/hexo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://mourning-soul.gitee.io/hexo"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-vue拦截器" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/hexo/2021/01/25/vue%E6%8B%A6%E6%88%AA%E5%99%A8/" class="article-date">
  <time class="dt-published" datetime="2021-01-25T10:00:00.000Z" itemprop="datePublished">2021-01-25</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      vue拦截器!
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="什么是VUE拦截器"><a href="#什么是VUE拦截器" class="headerlink" title="什么是VUE拦截器"></a>什么是VUE拦截器</h2><h3 id="拦截器分为请求拦截器和响应拦截器，在请求或响应被then或catch处理前拦截他们"><a href="#拦截器分为请求拦截器和响应拦截器，在请求或响应被then或catch处理前拦截他们" class="headerlink" title="拦截器分为请求拦截器和响应拦截器，在请求或响应被then或catch处理前拦截他们"></a>拦截器分为请求拦截器和响应拦截器，在请求或响应被then或catch处理前拦截他们</h3><hr>
<h2 id="拦截器的作用"><a href="#拦截器的作用" class="headerlink" title="拦截器的作用"></a>拦截器的作用</h2><ol>
<li><h3 id="添加统一的request的参数"><a href="#添加统一的request的参数" class="headerlink" title="添加统一的request的参数"></a>添加统一的request的参数</h3></li>
</ol>
<ul>
<li>比如客户端需要实现token的验证机制，比如你可以写$axios.get(‘/files’, params)，拦截器帮你拼接成 <a target="_blank" rel="noopener" href="http://127.0.0.1:8000/files">http://127.0.0.1:8000/files</a> 这样的请求地址</li>
</ul>
<ol start="2">
<li><h3 id="处理统一的responseError"><a href="#处理统一的responseError" class="headerlink" title="处理统一的responseError"></a>处理统一的responseError</h3></li>
</ol>
<ul>
<li> 比如重连机制，拿到error.code错误码重连，比如token过期，重新拿到token再次request </li>
<li> 比如统一报错信息，给所有返回的404来个提示也会很酷</li>
</ul>
<hr>
<h2 id="拦截器的实现"><a href="#拦截器的实现" class="headerlink" title="拦截器的实现"></a>拦截器的实现</h2><ul>
<li><h3 id="导包"><a href="#导包" class="headerlink" title="导包"></a>导包</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">import axios from <span class="string">&#x27;axios&#x27;</span></span><br></pre></td></tr></table></figure>
</li>
<li><h3 id="配置默认的host"><a href="#配置默认的host" class="headerlink" title="配置默认的host"></a>配置默认的host</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">axios.defuaults.baseURL = <span class="string">&#x27;http://127.0.0.1:8000&#x27;</span>   // 请求的接口域名</span><br></pre></td></tr></table></figure></li>
<li><h3 id="添加请求拦截器"><a href="#添加请求拦截器" class="headerlink" title="添加请求拦截器"></a>添加请求拦截器</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">// 添加请求拦截器(在请求前会自动去做的操作)，比如登录操作，如果登录了，那么自带token发送</span><br><span class="line">axios.interceptors.request.use(config =&gt; &#123;</span><br><span class="line">  <span class="built_in">let</span> token = localStorage.getItem(<span class="string">&#x27;token&#x27;</span>);</span><br><span class="line">  <span class="keyword">if</span> (token) &#123;</span><br><span class="line">    axios.defaults.headers[<span class="string">&#x27;Authorization&#x27;</span>] = token    //  头部设置  </span><br><span class="line">  &#125;</span><br><span class="line">　　<span class="built_in">return</span> config;</span><br><span class="line">&#125;, error =&gt; &#123;</span><br><span class="line">　　// 对请求错误做些什么</span><br><span class="line">    console.log(error)</span><br><span class="line">    <span class="built_in">return</span> Promise.reject(error)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></li>
<li><h3 id="添加响应拦截器"><a href="#添加响应拦截器" class="headerlink" title="添加响应拦截器"></a>添加响应拦截器</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">// 添加响应拦截器(在响应时自动回做的操作)，比如状态码返回为401是，要进入login（登录）界面</span><br><span class="line">axios.interceptors.response.use(response =&gt; &#123;</span><br><span class="line">　　// 对响应数据做点什么</span><br><span class="line">　　<span class="built_in">return</span> response</span><br><span class="line">&#125;, error =&gt; &#123;</span><br><span class="line">　　// 对响应错误做点什么</span><br><span class="line">　　<span class="built_in">return</span> error</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://mourning-soul.gitee.io/hexo/2021/01/25/vue%E6%8B%A6%E6%88%AA%E5%99%A8/" data-id="ckkt9opfx0008i4tw4es18r2d" data-title="vue拦截器!" class="article-share-link">Share</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/hexo/2021/01/26/vue%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          vue路由钩子
        
      </div>
    </a>
  
  
    <a href="/hexo/2021/01/21/hello-world/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Hello World</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/hexo/tags/blocker/" rel="tag">-blocker</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/hexo/tags/blocker/" style="font-size: 10px;">-blocker</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/02/">February 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/hexo/archives/2021/01/">January 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/hexo/2021/02/05/%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/">视频直播</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/04/%E6%97%A0%E9%99%90%E6%9E%81%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/">无限极分类自关联递归</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/03/webshell/">webshell</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/">redis键空间事件</a>
          </li>
        
          <li>
            <a href="/hexo/2021/02/01/redis%E5%8F%91%E5%B8%83%E8%80%85%E8%AE%A2%E9%98%85%E8%80%85/">redis发布者订阅者</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/hexo/" class="mobile-nav-link">Home</a>
  
    <a href="/hexo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/hexo/js/jquery-3.4.1.min.js"></script>



  
<script src="/hexo/fancybox/jquery.fancybox.min.js"></script>




<script src="/hexo/js/script.js"></script>





  </div>
</body>
</html>